package com.flyco.modules.admissionregistration.controller;

import java.util.ArrayList;
import java.util.Arrays;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.druid.util.StringUtils;
import com.alibaba.fastjson.JSONObject;
import com.flyco.common.api.vo.Result;
import com.flyco.common.system.query.QueryGenerator;
import com.flyco.common.aspect.annotation.AutoLog;
import com.flyco.common.system.vo.LoginUser;
import com.flyco.modules.admissionregistration.domain.AdmissionRegistrationExportVO;
import com.flyco.modules.admissionregistration.domain.AdmissionVisitorRegistrationExportVO;
import com.flyco.modules.admissionregistration.domain.EntourageDTO;
import com.flyco.modules.admissionregistration.domain.EntourageInfo;
import com.flyco.modules.admissionregistration.entity.AdmissionRegistration;
import com.flyco.modules.admissionregistration.service.IAdmissionRegistrationService;

import java.util.List;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.flyco.poi.excel.def.NormalExcelConstants;
import com.flyco.poi.excel.entity.ExportParams;
import com.flyco.poi.excel.view.EntityExcelView;
import lombok.extern.slf4j.Slf4j;
import com.flyco.common.system.base.controller.BaseController;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * @Description: 出入登记
 * @Author: flyco
 * @Date: 2023-06-02
 * @Version: V1.0
 */
@Slf4j
@Api(tags = "出入登记")
@RestController
@RequestMapping("/f/admissionregistration/admissionRegistration")
public class AdmissionRegistrationController extends BaseController<AdmissionRegistration, IAdmissionRegistrationService> {
    @Autowired
    private IAdmissionRegistrationService admissionRegistrationService;

    /**
     * 分页列表查询
     *
     * @param admissionRegistration
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @AutoLog(value = "出入登记-分页列表查询")
    @ApiOperation(value = "出入登记-分页列表查询", notes = "出入登记-分页列表查询")
    @GetMapping(value = "/workerList")
    public Result<?> queryWorkerListPageList(AdmissionRegistration admissionRegistration,
                                             @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                             @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                             HttpServletRequest req) {
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        QueryWrapper<AdmissionRegistration> queryWrapper = QueryGenerator.initQueryWrapper(admissionRegistration, req.getParameterMap());
        queryWrapper.in("purpose", "提货", "送货");
        if (admissionRegistration.getCreateTimeBegin() != null && admissionRegistration.getCreateTimeBegin() != null) {
            queryWrapper.between("create_time", admissionRegistration.getCreateTimeBegin(), admissionRegistration.getCreateTimeEnd());
        }
        Page<AdmissionRegistration> page = new Page<AdmissionRegistration>(pageNo, pageSize);
        IPage<AdmissionRegistration> pageList = admissionRegistrationService.page(page, queryWrapper);
        return Result.ok(pageList);
    }

    /**
     * 分页列表查询
     *
     * @param admissionRegistration
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @AutoLog(value = "来访登记-分页列表查询")
    @ApiOperation(value = "来访登记-分页列表查询", notes = "来访登记-分页列表查询")
    @GetMapping(value = "/visitorList")
    public Result<?> queryVisitorListPageList(AdmissionRegistration admissionRegistration,
                                              @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                              @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                              HttpServletRequest req) {
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        QueryWrapper<AdmissionRegistration> queryWrapper = QueryGenerator.initQueryWrapper(admissionRegistration, req.getParameterMap());
        queryWrapper.in("purpose", "来访", "面试");
        if (admissionRegistration.getCreateTimeBegin() != null && admissionRegistration.getCreateTimeBegin() != null) {
            queryWrapper.between("appointment_time", admissionRegistration.getCreateTimeBegin(), admissionRegistration.getCreateTimeEnd());
        }
        Page<AdmissionRegistration> page = new Page<AdmissionRegistration>(pageNo, pageSize);
        IPage<AdmissionRegistration> pageList = admissionRegistrationService.page(page, queryWrapper);

//        if (pageList != null && !CollectionUtils.isEmpty(pageList.getRecords())) {
//            pageList.getRecords().stream().map(o -> {
//                EntourageDTO haveEntourageDTO = JSONObject.parseObject(o.getEntourage(), EntourageDTO.class);
//                if (haveEntourageDTO != null && !CollectionUtils.isEmpty(haveEntourageDTO.getEntourageInfoList())) {
//                    StringBuilder VisitorEntourage = new StringBuilder();
//                    for (EntourageInfo entourageInfo : haveEntourageDTO.getEntourageInfoList()) {
//                        VisitorEntourage.append(entourageInfo.getEntourageName() + "|" + entourageInfo.getEntouragePhone());
//                    }
//                    o.setVisitorEntourage(VisitorEntourage.toString());
//                }
//                return o;
//
//            }).collect(Collectors.toList());
//        }
        return Result.ok(pageList);
    }

    /**
     * 添加
     *
     * @param admissionRegistration
     * @return
     */
    @AutoLog(value = "出入登记-添加")
    @ApiOperation(value = "出入登记-添加", notes = "出入登记-添加")
    @PostMapping(value = "/add")
    public Result<?> add(@RequestBody AdmissionRegistration admissionRegistration) {
        admissionRegistrationService.save(admissionRegistration);
        return Result.ok("添加成功！");
    }

    /**
     * 编辑
     *
     * @param admissionRegistration
     * @return
     */
    @AutoLog(value = "出入登记-编辑")
    @ApiOperation(value = "出入登记-编辑", notes = "出入登记-编辑")
    @PutMapping(value = "/edit")
    public Result<?> edit(@RequestBody AdmissionRegistration admissionRegistration) {
        admissionRegistrationService.updateById(admissionRegistration);
        return Result.ok("编辑成功!");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "出入登记-通过id删除")
    @ApiOperation(value = "出入登记-通过id删除", notes = "出入登记-通过id删除")
    @DeleteMapping(value = "/delete")
    public Result<?> delete(@RequestParam(name = "id", required = true) String id) {
        admissionRegistrationService.removeById(id);
        return Result.ok("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "出入登记-批量删除")
    @ApiOperation(value = "出入登记-批量删除", notes = "出入登记-批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public Result<?> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        this.admissionRegistrationService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.ok("批量删除成功！");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @AutoLog(value = "出入登记-通过id查询")
    @ApiOperation(value = "出入登记-通过id查询", notes = "出入登记-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<?> queryById(@RequestParam(name = "id", required = true) String id) {
        AdmissionRegistration admissionRegistration = admissionRegistrationService.getById(id);
        return Result.ok(admissionRegistration);
    }

    /**
     * 提货、送货导出excel
     *
     * @param request
     * @param admissionRegistration
     */
    @RequestMapping(value = "/exportWorkerList")
    public ModelAndView exportWorkerList(HttpServletRequest request, AdmissionRegistration admissionRegistration) {

        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();

        QueryWrapper<AdmissionRegistration> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("purpose", "提货", "送货");
        if (admissionRegistration.getCreateTimeBegin() != null && admissionRegistration.getCreateTimeBegin() != null) {
            queryWrapper.between("create_time", admissionRegistration.getCreateTimeBegin(), admissionRegistration.getCreateTimeEnd());
        }

        queryWrapper.orderByDesc("create_time");
        List<AdmissionRegistration> registrationList = admissionRegistrationService.list(queryWrapper);

        List<AdmissionRegistrationExportVO> exportVOS = new ArrayList<>();
        if (!CollectionUtils.isEmpty(registrationList)) {

            exportVOS = registrationList.stream().map(o -> {
                AdmissionRegistrationExportVO exportVO = new AdmissionRegistrationExportVO();
                BeanUtils.copyProperties(o, exportVO);
                exportVO.setStatus(o.getStatus() == 1 ? "未出园" : "已出园");
                return exportVO;
            }).collect(Collectors.toList());
        }

        //调用AutoPoi导出Excel
        ModelAndView mv = new ModelAndView(new EntityExcelView());
        mv.addObject(NormalExcelConstants.FILE_NAME, "出入登记报表");
        mv.addObject(NormalExcelConstants.CLASS, AdmissionRegistrationExportVO.class);
        mv.addObject(NormalExcelConstants.PARAMS, new ExportParams("出入登记报表", "导出人:" + sysUser.getRealname(), "出入登记报表"));
        mv.addObject(NormalExcelConstants.DATA_LIST, exportVOS);
        return mv;
    }

    /**
     * 来访导出excel
     *
     * @param request
     * @param admissionRegistration
     */
    @RequestMapping(value = "/exportVisitorList")
    public ModelAndView exportVisitorList(HttpServletRequest request, AdmissionRegistration admissionRegistration) {

        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();

        QueryWrapper<AdmissionRegistration> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("purpose", "来访", "面试");
        if (admissionRegistration.getCreateTimeBegin() != null && admissionRegistration.getCreateTimeBegin() != null) {
            queryWrapper.between("appointment_time", admissionRegistration.getCreateTimeBegin(), admissionRegistration.getCreateTimeEnd());
        }

        queryWrapper.orderByDesc("create_time");
        List<AdmissionRegistration> registrationList = admissionRegistrationService.list(queryWrapper);

        List<AdmissionVisitorRegistrationExportVO> exportVOS = new ArrayList<>();
        if (!CollectionUtils.isEmpty(registrationList)) {

            exportVOS = registrationList.stream().map(o -> {
                AdmissionVisitorRegistrationExportVO exportVO = new AdmissionVisitorRegistrationExportVO();
                BeanUtils.copyProperties(o, exportVO);
                exportVO.setStatus(o.getStatus() == 1 ? "未出园" : "已出园");
                return exportVO;
            }).collect(Collectors.toList());
        }

        //调用AutoPoi导出Excel
        ModelAndView mv = new ModelAndView(new EntityExcelView());
        mv.addObject(NormalExcelConstants.FILE_NAME, "来访登记报表");
        mv.addObject(NormalExcelConstants.CLASS, AdmissionVisitorRegistrationExportVO.class);
        mv.addObject(NormalExcelConstants.PARAMS, new ExportParams("来访登记报表", "导出人:" + sysUser.getRealname(), "来访登记报表"));
        mv.addObject(NormalExcelConstants.DATA_LIST, exportVOS);
        return mv;
    }


}
